from Login import models


# 隐藏用户的一些信息
class UserData:
    def __init__(self, user: dict):
        self.user = user

    def returnData(self):
        self.user.pop("password")

        return self.user


# 用户登录
def userLogin(email, password):
    if email is None:
        return {"msg": "请输入邮箱"}
    if password is None:
        return {"msg": "请输入密码"}

    user = models.user.objects.filter(email=email, password=password).values().first()
    if user:
        # 隐藏用户的一些信息
        userData = UserData(user=user).returnData()
        # print(userData)
        return {"msg": "登录成功", "data": userData}
    else:
        return {"msg": "邮箱或者密码错误,请确认是否注册过账号"}


# 将验证码状态设为失效
def makeCodeBeInvalid(email):
    # print(email)
    code = models.Code.objects.filter(email=email).order_by('-create_time').first()
    # print(code,"=================")
    if code:
        code.status = 0
        code.save()
    else:
        return {"msg": "验证码不存在"}
